//
//  SegmentView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/6/7.
//

#import "SegmentView.h"
#import "CommonUtil.h"


@interface SegmentView()
@property (strong, nonatomic) UIButton *selectBtn;
@end

@implementation SegmentView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = AD(10);
        self.clipsToBounds = YES;
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = AD(1);
    }
    return self;
}

-(void)setTitles:(NSArray *)titles{
    _titles = titles;
    CGFloat w = self.width/titles.count;
    for (NSInteger i = 0; i < titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:AD(15)];
        btn.frame = CGRectMake(i * w, 0, w, self.height);
        [self addSubview:btn];
        if (i == 0) {
            [self btnClick:btn];
        }
    }
    
}


- (void)btnClick:(UIButton *)sender{
    self.selectBtn.selected = false;
    self.selectBtn.backgroundColor = [UIColor clearColor];
    self.selectBtn = sender;
    self.selectBtn.selected = YES;
    self.selectBtn.backgroundColor = RGB(255, 91, 117);
}

@end
